package com.turing.post.thirdinterface.impl;

import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.turing.post.thirdinterface.util.MapActivateCardDao;
import com.turing.post.util.PCPSApplicationException;

@Stateless(mappedName = "thirdinterface.impl.MapActivateCardDaoImpl")
@Remote
@Local
public class MapActivateCardDaoImpl implements MapActivateCardDao {

	/**
	 * 实体管理器
	 */
	@PersistenceContext
	protected EntityManager _em;

	/**
	 * 删除激活卡映射表数据 初始化中使用
	 */
	@Override
	public void deleteMapActivateCard() {
		String sql = "delete from mapactivatecard";
		Query query = _em.createNamedQuery(sql);
		query.executeUpdate();
	}

	/**
	 * 获取最大卡序号从map表中
	 * 
	 * @return
	 */
	@Override
	public String getMaxCardSeqNoFromMapActivateCard(String batchNo) {
		String sql = "select max(t.cardseqno) from mapactivatecard t where t.cardseqno like ?1";
		Query query = _em.createNativeQuery(sql);
		query.setParameter(1, batchNo + "%");
		return (String) query.getSingleResult();
	}

	/**
	 * 向map表中插入老表中的卡序号
	 */
	@Override
	public void insertOldActivateCardSeqNoToMap(String cardSeqNo)
			throws PCPSApplicationException {
		String sql = "insert into mapactivatecard(cardseqno) values(?1)";
		Query query = _em.createNativeQuery(sql);
		query.setParameter(1, cardSeqNo);
		query.executeUpdate();
	}

}
